<?php
namespace app\admin\controller;
use think\Request;
use think\Image;
class Shop extends Common
{
    public function product_list()
    {

        $pagenum=3;
        $db=db('shoper');
        $cat=db('cat')->select();
        $tmp_cat=$cat;
        foreach ($tmp_cat as $key1 => $value1) {
            foreach ($tmp_cat as $key2 => $value2) {
                if($value1['id']==$value2['pid']){
                    $cat[$key1]['son'][]=$value2;
                    unset($cat[$key2]);
                }         
            }
        }
        $search=input('get.search','','htmlspecialchars');
        $cat_search=input('get.cat_search','','htmlspecialchars');
        if($cat_search){
            $db->where('cat_id','like','%'.$cat_search.'%');
            $page_search=$cat_search;
        }else{
            $page_search=$search;
        }
        if($search){    
        
            $db->where('shopname|price','like','%'.$search.'%');
            
            // ini_set('memory_limit','1024M');
       
            // require EXTEND_PATH.'jieba/vendor/multi-array/MultiArray.php';
            // require EXTEND_PATH.'jieba/vendor/multi-array/Factory/MultiArrayFactory.php';
            // require EXTEND_PATH.'jieba/class/Jieba.php';
            // require EXTEND_PATH.'jieba/class/Finalseg.php';
        
            // \Fukuball\Jieba\Jieba::init();
            // \Fukuball\Jieba\ Finalseg::init();
            // $search = \Fukuball\Jieba\Jieba::cut($search);
         
            // for ($i = 0; $i<count($search) ; $i++){
            //     $search[$i] = '%'.$search[$i].'%';
            // }

            // $where = '';
            // for ($i = 0; $i<count($search) ; $i++){
            //     $where .= ' `ord` LIKE "'.$search[$i]. '" OR ';
            // }
            // $where = rtrim($where , 'OR ');
            // $db->where($where);
    
        }
        $shoper_list=$db->paginate($pagenum,false,['query'=>['search'=>$page_search]]);
        $page=$shoper_list->render();
        $page=str_replace(
            ['class="pagination"','class="disabled"','class="active"'],
            ['class="am-pagination tpl-pagination"','class="am-disabled"','class="am-active"'],
            $page
        );
        $current_page=input('get.page','1','intval');
        $request=Request::instance();
        if($request->isAjax()){
            return json(['shoper_list'=>$shoper_list,'page'=>$page,'current_page'=>$current_page]);
        }
        $this->assign('cat',$cat);        
        $this->assign('page' , $page);
        $this->assign('shoper_list',$shoper_list);
        $this->assign('current_page' , $current_page);
        return view();
    }
    public function product_category()
    {
        $pagenum=2;
        $start=0;
        $data=array();
        $cat_son_list=db('cat')->order('desc')->select();
        $tmp_caidan=$cat_son_list;
        foreach ($tmp_caidan as $key1 => $value1) {
            foreach ($tmp_caidan as $key2 => $value2) {
                if($value1['id']==$value2['pid']){
                    $cat_son_list[$key1]['son'][]=$value2;
                    unset($cat_son_list[$key2]);
                }         
            }
            
        }
        foreach($cat_son_list as $key => $value){
            $data[]=$value;
        }
        $now_page=input('get.now_page');
        $pages=count($data);        
        if($now_page){
            $start=($now_page-1)*$pagenum;
            $new_data=array_slice($data,$start,$pagenum);
        }else{
            $new_data=array_slice($data,$start,$pagenum);
        }
        $cat_list=db('cat')->where('pid',0)->order('desc')->paginate($pagenum);
        $cat_son_list=json_encode($cat_son_list,JSON_UNESCAPED_UNICODE);
        $page=$cat_list->render();
        $page=str_replace(
            ['class="pagination"','class="disabled"','class="active"'],
            ['class="am-pagination tpl-pagination"','class="am-disabled"','class="am-active"'],
            $page
        );
        $current_page=input('get.page','1','intval');
        $request=Request::instance();
        if($request->isAjax()){
            return json(['cat_son_list'=>$new_data,'page'=>$page,'current_page'=>$current_page]);
        }
        $this->assign('page' , $page);
        $this->assign('cat_son_list' , $cat_son_list);
        $this->assign('cat_list',$cat_list);
        $this->assign('current_page' , $current_page);
        return view();
    }
    public function delect(){
        $id=input('get.id');
        $info = db('shoper')->find($id);
        if(file_exists(ROOT_PATH . 'public' . DS . 'uploads/'.$info['src']) && is_file(ROOT_PATH . 'public' . DS . 'uploads/'.$info['src'])){
            unlink(ROOT_PATH . 'public' . DS . 'uploads/'.$info['src']);
        }
        if(file_exists(ROOT_PATH . 'public' . DS . 'uploads/thumb/'.$info['src']) && is_file(ROOT_PATH . 'public' . DS . 'uploads/thumb/'.$info['src'])){
            unlink(ROOT_PATH . 'public' . DS . 'uploads/thumb/'.$info['src']);
        }
        $infos = db('shoper_detail_img')->where('shoper_id',$id)->select();
        foreach ($infos as $key => $value) {
            if(file_exists(ROOT_PATH . 'public' . DS . 'uploads/'.$value['src']) && is_file(ROOT_PATH . 'public' . DS . 'uploads/'.$value['src'])){
                unlink(ROOT_PATH . 'public' . DS . 'uploads/'.$value['src']);
            }
            if(file_exists(ROOT_PATH . 'public' . DS . 'uploads/thumb/'.$value['src']) && is_file(ROOT_PATH . 'public' . DS . 'uploads/thumb/'.$value['src'])){
                unlink(ROOT_PATH . 'public' . DS . 'uploads/thumb/'.$value['src']);
            }
        }    
        db('shoper')->where('id',$id)->delete();
        db('shoper_detail_img')->where('shoper_id',$id)->delete();
        db('shoper_specifications')->where('product_id',$id)->delete();
        db('shoper_specifications_attribute')->where('product_id',$id)->delete();
        db('shoper_fixed_attr')->where('product_id',$id)->delete();
    }
    public function add_category(){
        $cat=db('cat')->select();
        $tmp_cat=$cat;
        foreach ($tmp_cat as $key1 => $value1) {
            foreach ($tmp_cat as $key2 => $value2) {
                if($value1['id']==$value2['pid']){
                    $cat[$key1]['son'][]=$value2;
                    unset($cat[$key2]);
                }         
            }
            
        }
        $this->assign('cat',$cat);
        return view();
    }
    public function edit_category(){
        $post=input('post.');
        $cat=validate('cat');           
        if(!$cat->check($post)){
            $this->error($cat->getError());
        }
        $id=db('cat')->insert($post);
        if($id){
            $this->success('添加成功','product_category');
        }else{
            $this->error('添加失败');
        }
    }
    public function category_delect(){
        $id=input('get.id');
        db('cat')->delete($id);
        db('cat')->where('pid',$id)->delete();
    }
    public function add_product()
    {
        $cat=db('cat')->select();
        $tmp_cat=$cat;
        foreach ($tmp_cat as $key1 => $value1) {
            foreach ($tmp_cat as $key2 => $value2) {
                if($value1['id']==$value2['pid']){
                    $cat[$key1]['son'][]=$value2;
                    unset($cat[$key2]);
                }         
            }
            
        }
        $this->assign('cat',$cat);
        return view();
    }
    public function edit_product(){
        $id=input('get.id');
        $cat=db('cat')->select();
        $tmp_cat=$cat;
        foreach ($tmp_cat as $key1 => $value1) {
            foreach ($tmp_cat as $key2 => $value2) {
                if($value1['id']==$value2['pid']){
                    $cat[$key1]['son'][]=$value2;
                    unset($cat[$key2]);
                }         
            }
            
        }
        $res=db('shoper')->alias('a')->field('a.*,b.name as groupname, c.src as detailsrc')->join('cat b','a.cat_id=b.id')->join('shoper_detail_img c','a.id=c.shoper_id')->where('a.id',$id)->select();
        $this->assign('res',$res);
        $this->assign('cat',$cat);
        return view();
    }
    public function product_edit(){
        $post['shopname'] = input('post.shopname' , '' , 'htmlspecialchars');
        $post['price'] = input('post.price' , '' , 'htmlspecialchars');
        $post['discount'] = input('post.discount' , '' , 'htmlspecialchars');
        $post['stoke'] = input('post.stoke' , '' , 'htmlspecialchars');
        $post['detail'] = input('post.detail' , '' , 'htmlspecialchars');
        $post['cat_id'] = input('post.cat_id' , '' , 'htmlspecialchars');
        $id = input('post.updateid' , '' , 'htmlspecialchars');
        $product=validate('product');           
        if(!$product->check($post)){
            $this->error($product->getError());
        }
        $file = request()->file('src');
        if(!$file){
            unset($post[7]);
        }
        if($file){
            $info = db('shoper')->find($id);
            if(file_exists(ROOT_PATH . 'public' . DS . 'uploads/'.$info['src']) && is_file(ROOT_PATH . 'public' . DS . 'uploads/'.$info['src'])){
                unlink(ROOT_PATH . 'public' . DS . 'uploads/'.$info['src']);
            }
            if(file_exists(ROOT_PATH . 'public' . DS . 'uploads/thumb/'.$info['src']) && is_file(ROOT_PATH . 'public' . DS . 'uploads/thumb/'.$info['src'])){
                unlink(ROOT_PATH . 'public' . DS . 'uploads/thumb/'.$info['src']);
            }
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $post['src']  = $info->getSaveName();
                $image  = Image::open(ROOT_PATH . 'public' . DS . 'uploads/' . $post['src']);
                if (!file_exists(dirname(ROOT_PATH . 'public' . DS .'/uploads/thumb/'. $post['src']))){
                    mkdir(dirname(ROOT_PATH . 'public' . DS .'/uploads/thumb/'. $post['src']) , 0777, true);
                }
                $image->thumb('100' , '100' , Image::THUMB_FILLED) -> save( ROOT_PATH . 'public' . DS .'/uploads/thumb/'. $post['src'] , null , 100);
            }else{
                $this->error( $file->getError() );
            }
        }
        $re=db('shoper')->where('id',$id)->update($post);

        $files = request()->file('image');
        if($files){
            $infos = db('shoper_detail_img')->where('shoper_id',$id)->select();
            foreach ($infos as $key => $value) {
                if(file_exists(ROOT_PATH . 'public' . DS . 'uploads/'.$value['src']) && is_file(ROOT_PATH . 'public' . DS . 'uploads/'.$value['src'])){
                    unlink(ROOT_PATH . 'public' . DS . 'uploads/'.$value['src']);
                }
                if(file_exists(ROOT_PATH . 'public' . DS . 'uploads/thumb/'.$value['src']) && is_file(ROOT_PATH . 'public' . DS . 'uploads/thumb/'.$value['src'])){
                    unlink(ROOT_PATH . 'public' . DS . 'uploads/thumb/'.$value['src']);
                }
            }   
            db('shoper_detail_img')->where('shoper_id',$id)->delete();
            foreach($files as $file){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    $date['src']  = $info->getSaveName();
                    $date['shoper_id']=$id;
                    $res=db('shoper_detail_img')->insert($date);
                    $image  = Image::open(ROOT_PATH . 'public' . DS . 'uploads/' . $date['src']);
                    if (!file_exists(dirname(ROOT_PATH . 'public' . DS .'/uploads/thumb/'. $date['src']))){
                        mkdir(dirname(ROOT_PATH . 'public' . DS .'/uploads/thumb/'. $date['src']) , 0777, true);
                    }
                    $image->thumb('100' , '100' , Image::THUMB_FILLED) -> save( ROOT_PATH . 'public' . DS .'/uploads/thumb/'. $date['src'] , null , 100);
                }else{
                    $this->error( $file->getError() );
                }
            }          
        }
        if($re||$res){
            $this->success('更新成功','product_list');
        }else{
            $this->error('您得更新些啥');
        }
    }
    public function add_product_list()
    {
        $post['shopname'] = input('post.shopname' , '' , 'htmlspecialchars');
        $post['price'] = input('post.price' , '' , 'htmlspecialchars');
        $post['discount'] = input('post.discount' , '' , 'htmlspecialchars');
        $post['stoke'] = input('post.stoke' , '' , 'htmlspecialchars');
        $post['detail'] = input('post.detail' , '' , 'htmlspecialchars');
        $post['cat_id'] = input('post.cat_id' , '' , 'htmlspecialchars');
        $product=validate('product');           
        if(!$product->check($post)){
            $this->error($product->getError());
        }
        $file = request()->file('src');    
        if(!$file){
            $this->error('产品封面图不能为空');
        }    
        $files = request()->file('image');
        if(!$files){
            $this->error('产品主图不能为空');
        }    
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');

            if($info){
                $post['src']  = $info->getSaveName();
                $image  = Image::open(ROOT_PATH . 'public' . DS . 'uploads/' . $post['src']);
                if (!file_exists(dirname(ROOT_PATH . 'public' . DS .'/uploads/thumb/'. $post['src']))){
                    mkdir(dirname(ROOT_PATH . 'public' . DS .'/uploads/thumb/'. $post['src']) , 0777, true);
                }
                $image->thumb('100' , '100' , Image::THUMB_FILLED) -> save( ROOT_PATH . 'public' . DS .'/uploads/thumb/'. $post['src'] , null , 100);
            }else{
                $this->error( $file->getError() );
            }
        }
        $id=db('shoper')->insertgetid($post);
        if($id){
        foreach($files as $file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $date['src']  = $info->getSaveName();
                $date['shoper_id']=$id;
                db('shoper_detail_img')->insert($date);
                $image  = Image::open(ROOT_PATH . 'public' . DS . 'uploads/' . $date['src']);
                if (!file_exists(dirname(ROOT_PATH . 'public' . DS .'/uploads/thumb/'. $date['src']))){
                    mkdir(dirname(ROOT_PATH . 'public' . DS .'/uploads/thumb/'. $date['src']) , 0777, true);
                }
                $image->thumb('100' , '100' , Image::THUMB_FILLED) -> save( ROOT_PATH . 'public' . DS .'/uploads/thumb/'. $date['src'] , null , 100);
            }else{
                $this->error( $file->getError() );
            }
        }
            $this->success('添加成功','add_product');
        }else{
            $this->error('添加失败');
        }
    }
    public function guige () {
        $id = input('get.id');
        $pg = model('ProductGuige');
        $guige = $pg->getProductGuige($id);

        $this->assign('guige' , $guige);
        return view();
    }
    
    public function guige_store () {
        $name = input('get.name');
        $guige_list = json_decode($name , true);
        foreach($guige_list as $key => $value) {
            db('ShoperSpecifications')->where('guige_id' , $value['guige_id'])->update($value);
        }
    }

    public function guige_add () {
        $data['product_id'] = input('get.product_id');
        $data['pid'] = input('get.pid');
        $id = db('ShoperSpecifications')->insertGetId($data);
        if ($id) {
            return json(['msg'=> 'ok' , 'guige_id'=>$id]);
        }else {
            return json(['msg'=>'fail']);
        }
    }

    public function guige_del () {
        $guige_id = input('get.guige_id');
        $erjiguige = db('ShoperSpecifications')->where('pid' , $guige_id)->select();
        db('ShoperSpecificationsAttribute')->where('guige_id' , $guige_id)->delete();
        foreach($erjiguige as $key => $value) {
            db('ShoperSpecifications')->where('pid' , $value['guige_id'])->delete();
            db('ShoperSpecifications')->where('guige_id' , $value['guige_id'])->delete();
            db('ShoperSpecificationsAttribute')->where('guige_id' , $value['guige_id'])->delete();
        }
        db('ShoperSpecifications')->where('guige_id' , $guige_id)->delete();
    }

    public function guige_xiang_add () {
        $data['guige_id'] = input('get.guige_id');
        $data['product_id'] = input('get.product_id');
        $id = db('ShoperSpecificationsAttribute')->insertGetId($data);
        if ($id) {
            return json(['msg'=> 'ok' , 'attr_id'=>$id]);
        }else {
            return json(['msg'=>'fail']);
        }
    }

    public function guige_attr_store () {
        $name = input('get.name');
        $attr_list = json_decode($name , true);
        foreach($attr_list as $key => $value) {
            db('ShoperSpecificationsAttribute')->where('attr_id' , $value['attr_id'])->update($value);
        }
    }

    public function guige_attr_del () {
        $guige_id = input('get.attr_id');
        db('ShoperSpecificationsAttribute')->where('attr_id' , $guige_id)->delete();
    }
    public function guding_store(){
        $post=input('post.');
        $insert= model('ProductGuige');
        $result=$insert->insert($post);
        if($result){
            $this->success('保存成功',url('shop/product_list'));
        };
    }
}
